home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp48hor1 / prime.doc < prev    next >
Text File  |  1995-03-31  |  2KB  |  66 lines

  1. HP 48SX prime factorizer routines, inspired by HP 41 PPC ROM's "NP": 
  2.  
  3.   Name   Description              In            Out 
  4. +------+------------------------+--------+----+----------------+ 
  5. | NP   | Next Prime factor      | n      | -> | n    factor    | 
  6. +------+------------------------+--------+----+----------------+ 
  7.  
  8. [#5AD9h, 122.5 w/ name] -- see pg. 627 for character translations. 
  9.  
  10. \<< DUP \v/ \-> s 
  11.   \<< DUP 
  12.     IF 2 MOD 
  13.     THEN 3 
  14.       WHILE DUP2 MOD OVER s < AND 
  15.       REPEAT 2 + 
  16.       END DUP s 
  17.       IF > 
  18.       THEN DROP DUP 
  19.       END 
  20.     ELSE 2 
  21.     END 
  22.   \>> 
  23. \>>  @ end of NP 
  24.  
  25.   Name   Description              In            Out 
  26. +------+------------------------+--------+----+---------------------+ 
  27. | PF   | Prime Factors          | n      | -> | n: { factors of n } | 
  28. +------+------------------------+--------+----+---------------------+ 
  29.  
  30. [#445Bh, 62 w/ name] 
  31.  
  32. \<< DUP { } SWAP 
  33.   DO NP ROT OVER + ROT ROT / DUP 
  34.   UNTIL 1 == 
  35.   END DROP SWAP \->TAG 
  36. \>>  @ end of PF 
  37.  
  38. To use NP, enter any integer n (greater than 0) and press NP.  The 
  39. original n will be raised to level 2, and the first prime factor of n 
  40. will be placed in level 1. 
  41.  
  42. To find the next factor, you can press / and then run NP again. 
  43.  
  44. If you want to find all the prime factors of n, run PF.  It replaces 
  45. n with a list of its prime factors. 
  46.  
  47. Some sample PF times: 
  48. 2 -> { 2 } in .09 sec 
  49. 153 -> { 3 3 17 } in .33 sec 
  50. 12345 -> { 3 5 823 } in .59 sec 
  51. 1234567 -> { 127 9721 } in 2.47 sec 
  52. 123456789 -> { 3 3 3607 3803 } in 40.19 sec 
  53.  
  54. Can the Saturn code in the HP 71 JPC Rom's "PRIM" be ported to the 
  55. 48SX? 
  56.  
  57. Note to all you 48 experts: this is my first attempt at programming 
  58. the 48, so forgive the inelegancies, like that horrible ROT OVER ROT 
  59. ROT stuff.  It seems to me that using STO and RCL for temporary storage 
  60. slows a program down horribly, but elegant stack management is an art, 
  61. not a science.  If a way of improving these routines seems relatively 
  62. intuitively obvious to you, post it, and help us BASIC spaghetti-coders 
  63. learn "structured" programming...  Thanx! 
  64.  
  65. Joseph K. Horn - 1042 Star Route - Orange, CA 92667 - (714) 858-0920 
  66.